/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution
{
public:
    ListNode *detectCycle(ListNode *head)
    {
        unordered_set<ListNode *> visited;
        auto node = head;
        while (node != nullptr)
        {
            if (visited.count(node))
            {
                return node;
            }
            visited.insert(node);
            node = node->next;
        }
        return nullptr;
    }
};